package com.example.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.core.constants.HttpConstants;
import com.example.core.entity.LoginUser;
import com.example.core.enums.ResultCode;
import com.example.core.enums.UserIdentity;
import com.example.core.result.Res;
import com.example.redis.service.RedisService;
import com.example.security.exception.ServiceException;
import com.example.security.service.TokenService;
import com.example.system.entity.DO.SysUserDO;
import com.example.system.entity.DTO.AddUserDTO;
import com.example.system.entity.DTO.LoginDTO;
import com.example.system.entity.VO.SysUserInfoVO;
import com.example.system.mapper.SysUserMapper;
import com.example.system.service.ISysUserService;
import com.example.system.utils.BcryptUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

@Service
@RefreshScope
public class SysUserServiceImpl implements ISysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private RedisService redisService;
    @Autowired
    private TokenService tokenService;

    @Value("${jwt.secret}")
    private String secret;

    @Override
    public Res<String> login(LoginDTO loginDTO) {
        LambdaQueryWrapper<SysUserDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserDO::getAccount, loginDTO.getUserAccount());
        SysUserDO sysUserDO = sysUserMapper.selectOne(wrapper);
        if (sysUserDO == null) {
            return Res.fail(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        if (BcryptUtils.matchesPassword(loginDTO.getPassword(), sysUserDO.getPassword())) {
            LoginUser loginUser = new LoginUser(sysUserDO.getId(), UserIdentity.ADMIN.getValue(), sysUserDO.getNickname());
            String token = tokenService.createToken(loginUser, secret);
            return Res.ok(token);
        } else {
            return Res.fail(ResultCode.FAILED_PASSWORD);
        }
    }


    @Override
    public boolean logout(String token) {
        token = tokenService.parseToken(token);
        return tokenService.deleteLoginUser(token, secret);
    }


    @Override
    public int add(AddUserDTO addUserDTO) {
        SysUserDO addUser = new SysUserDO();
        LambdaQueryWrapper<SysUserDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserDO::getAccount, addUserDTO.getAccount());
        SysUserDO existUser = sysUserMapper.selectOne(wrapper);
        if (existUser != null) {
            throw new ServiceException(ResultCode.AILED_USER_EXISTS);
        }

        addUser.setAccount(addUserDTO.getAccount());
        addUser.setPassword(BcryptUtils.encryptPassword(addUserDTO.getPassword()));
        return sysUserMapper.insert(addUser);
    }


    @Override
    public Res<SysUserInfoVO> getUserInfo(String token) {
        token = tokenService.parseToken(token);
        LoginUser loginUser = tokenService.getUserInfo(token, secret);
        if (loginUser == null) {
            return Res.fail();
        }
        SysUserInfoVO sysUserInfoVO = new SysUserInfoVO();
        sysUserInfoVO.setNickname(loginUser.getNickname());
        return Res.ok(sysUserInfoVO);
    }
}
